package com.dog.auth.web;

import com.dog.auth.domain.dto.request.LoginReqDTO;
import com.dog.auth.domain.dto.request.RegisterReqDTO;
import com.dog.auth.domain.vo.AuthUserVO;
import com.dog.auth.service.AuthService;
import com.dog.framework.core.model.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/auth")
public class AuthController {


    @Autowired
    private AuthService authService;



    /**
     * 刷新token
     * @return
     */
    @GetMapping("/image/code")
    public Map<String, Object> createCaptcha() throws IOException {
        return authService.createCaptcha();
    }


    /**
     * 登录
     * @param loginReqDTO
     * @return
     */
    @PostMapping("/login")
    public AuthUserVO login(@RequestBody @Valid LoginReqDTO loginReqDTO) {
        return authService.login(loginReqDTO);
    }

    /**
     * 注册
     * @param dto
     * @return
     */
    @PostMapping("/register")
    public boolean register(@RequestBody RegisterReqDTO dto) {
        // 用户注册
        return authService.register(dto);
    }

    /**
     * 登出
     * @param
     * @return
     */
    @DeleteMapping("/logout")
    public boolean logout() {
        // 用户登出
        return authService.logout();
    }

    /**
     * 刷新token
     * @param request
     * @return
     */
    @PostMapping("refresh")
    public ResponseResult refresh(HttpServletRequest request) {
        return ResponseResult.success();
    }


}
